package com.tools.cleanmaster.ui.notification;


import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.scenenew.SceneConstants;
import com.tools.cleanmaster.scenenew.ScenePriority;
import com.tools.cleanmaster.scenenew.SceneType;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.NetworkUtils;

/**
 * 通知栏Item基类，提供展示判断接口和通知栏生成接口，
 * 详细判断逻辑和通知栏文案、跳转intent由子类实现。
 */
public abstract class NotificationBaseItem {
    public static final String TAG = "Scene";
    public static final String SCENE_TYPE_EXTRA_FLAG = "com.optimizer.module.scene.scene_type";


    /**
     * 判断通知是否展示，多个条件，将耗时操作靠后放
     */
    public boolean checkRules() {
        //判断网络
        if (!NetworkUtils.isNetworkAvaialble(CleanApplication.getInstance())) {
            return false;
        }
        //判断自身间隔时间
        if (!isTimeValid()) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "场景不展示，checkRules -> isTimeValid: " + false);
            }
            return false;
        }
        //判断自身条件阀值
        if (!isSceneValid()) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "场景不展示，checkRules -> isSceneValid: " + false);
            }
            return false;
        }
        //判断生效时间和优先级
        if (!checkEffectiveTime()) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "场景不展示，checkRules -> checkEffectiveTime: " + false);
            }
            return false;
        }
        //判断广告有效性
        if (!isAdValid()) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "场景不展示，checkRules -> isAdValid: " + false);
            }
            return false;
        }
        return true;
    }

    /**
     * 低优先级替换高优先级通知，需要判断生效时间
     */
    private boolean checkEffectiveTime() {
        if (getSceneType().priority == ScenePriority.A_PLUS) {
            return true;
        } else {
            long lastNotificationShow = SceneSharedPrefManager.getLastNotificationShowTime();
            long validTime = SceneSharedPrefManager.getNotificationValidTime() * SceneConstants.HOUR_MS;
            SceneType lastShowType = SceneSharedPrefManager.getCurrentShowSceneType();
            if (lastShowType != null && lastShowType.priority != getSceneType().priority) {
                if (System.currentTimeMillis() - lastNotificationShow < validTime
                        && getSceneType().priority.ordinal() > lastShowType.priority.ordinal()) {
                    if (FeatureConfig.DEBUG_LOG) {
                        LogHelper.d(TAG, "不同优先级，在生效时间内优先级太低不展示 " + getSceneType() + " last:" + lastShowType);
                    }
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * 判断广告有效性
     */
    public boolean isAdValid() {
        Context context = CleanApplication.getInstance();

        //场景化首次跳转带量页，不判断广告
        SceneType sceneType = getSceneType();
        if (SceneType.BAT_SHARPDEC == sceneType || SceneType.BG_MEM_OVERLOAD == sceneType) {
            boolean isFirst = SceneSharedPrefManager.isFirstInScene(sceneType.key);
            if (isFirst) {
                return true;
            }
        }

        /*if (CardDataHelper.shouldRefreshAd(context, EntranceType.LANDING)) {
            //int adValidCount = CardDataHelper.getAdValidCount(AdConstants.SID_LANDING);
            int adValidCount = 0;
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "--------第一次判断，有效广告数量：" + adValidCount);
            }
            if (adValidCount <= 0) {
                CardDataHelper.requestAd(EntranceType.LANDING);
                int timeout = SceneSharedPrefManager.getSceneAdTimeout(context);
                SystemClock.sleep(timeout * 1000);
                //adValidCount = CardDataHelper.getAdValidCount(AdConstants.SID_LANDING);
                if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.i(TAG, "--------第二次判断，有效广告数量：" + adValidCount);
                }
                return adValidCount > 0;
            } else {
                return true;
            }
        }*/
        return SceneSharedPrefManager.isSceneAdNoJudge(context);
    }

    /**
     * 判断展示条件
     */
    abstract public boolean isSceneValid();

    /**
     * 创建通知栏之前准备数据
     */
    abstract public Notification prepareNotification();

    abstract public SceneType getSceneType();

    /**
     * 该场景化是否处于自己的有效时间间隔内
     */
    abstract public boolean isTimeValid();


    /**
     * 通知栏UI参数
     */
    public static class UIParam {
        public int tickerIcon = R.mipmap.ic_launcher;
        public int leftIconResId = R.mipmap.ic_launcher;
        public Bitmap leftIconBitmap;
        public CharSequence title;
        public int titleColor;
        public CharSequence content;
        public int contentColor;
        public CharSequence btnText;
        public CharSequence tickerText;
        public boolean isHasRightPart = true;
        public int rightIcon;
        public CharSequence rightText;
        public CharSequence bigPicBtnText;
        public int backgroundAppNum;
        public CharSequence iconText;
        public int iconTextColor;

        /**
         * notification实际执行的intent,目前只支持startActivity的Intent
         */
        public Intent contentIntent;

        public SceneType sceneType;
        public int viewType;

        public CharSequence leftText;
    }

    protected void log(String message) {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "----场景化类型 " + getSceneType() + " " + message);
        }
    }
}
